﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc4Learn.Models;
using Mvc4Learn.Models.ViewModels;

namespace Mvc4Learn.Controllers
{
    public partial class MusicStoreController : Controller
    {
        #region 第二种编辑页面的方法
        /*/// <summary>
        /// 另外一种Post方法，
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit()
        {
            var album = new Album();
            if (TryUpdateModel(album))
            {
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
                //特别注意，这里不能直接return view，不然，Model就是空的。
                //                return View("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        } 
*/
        #endregion

        [HttpPost]
        public ActionResult Edit(AlbumViewModel vmodel)
        {
            try
            {
                var album = new Album()
                    {
                        AlbumArtUrl = vmodel.Album.AlbumArtUrl,
                        AlbumId = vmodel.Album.AlbumId,
                        ArtistId = vmodel.Artist.ArtistId,
                        GenreId = vmodel.Genre.GenreId,
                        Price = vmodel.Album.Price,
                        Title = vmodel.Album.Title
                    };
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                //            ModelState.AddModelError("", "this is all wrong");
                //            ModelState.AddModelError("", "what a sad name");
                //            ModelState.AddModelError("test", "what a sad test");
                //            ModelState.AddModelError("Title", "what a sad Title");
                //            ModelState.AddModelError("", "what a sad");

                ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", vmodel.Album.GenreId);
                ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", vmodel.Album.ArtistId);
                return View(vmodel);
            }
        }

        /// <summary>
        /// 通过get方式来创建搜索
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public ActionResult Search(string search)
        {
            var albums = db.Albums.Include(a => a.Artist).Include(a => a.Genre).Where(x => x.Title.Contains(search)).ToList();
            //回到Index视图
            return View("Index", albums);
        }
    }
}
